package model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@NamedQueries({
        @NamedQuery(
                name = "Person.findByLastName",
                query = "SELECT p FROM Person p WHERE p.lastName = :lastName"
        ),
        @NamedQuery(
                name = "Person.findAll",
                query = "SELECT p FROM Person p"
        ) ,
        @NamedQuery(
                name = "Person.findByFirstName",
                query = "SELECT p FROM Person p WHERE p.firstName = :firstName"
        )
})
public class Person {
    private long id;
    private String firstName;
    private String lastName;
    private String description;
    private byte[] image;
    private List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
    private List<Address> addresses = new ArrayList<Address>();

    public Person() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    @OneToMany(mappedBy = "person")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    @ManyToMany(mappedBy = "persons")
    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;

        Person person = (Person) o;

        if (id != person.id) return false;
        if (description != null ? !description.equals(person.description) : person.description != null) return false;
        if (!firstName.equals(person.firstName)) return false;
        if (!lastName.equals(person.lastName)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + firstName.hashCode();
        result = 31 * result + lastName.hashCode();
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }
}
